<?xml version="1.0" encoding="UTF-8"?>
<!--
  Created by IntelliJ IDEA.
  User: Nikita.Brazhnikov
  Date: 4/30/13
  Time: 1:02 PM
-->
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/facelets"
      xmlns:c="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui" xmlns:fn="http://java.sun.com/jstl/functions" xmlns:cc="http://java.sun.com/jsf/composite"
      xmlns:pe="http://primefaces.org/ui/extensions" xmlns:of="http://omnifaces.org/functions" xmlns:o="http://omnifaces.org/ui" xmlns:ui="http://java.sun.com/jstl/core">

<h:head>
    <title>Log viewer</title>
    <h:outputScript>
        function clearFilters() {
            $('#files_form\\:files\\:fname_col\\:filter').val('')
        }

        function updateContextMenu() {
            PrimeFaces.ajax.AjaxRequest({
                formId: "files_form",
                process: "files",
                update: "cmenu"
            });
        }
    </h:outputScript>

</h:head>
<h:outputStylesheet library="css" name="css.css"/>
<o:importFunctions type="ru.bragnikita.utils.logexplorer.web.ConvertationUtils" var="cu"/>
<h:body>
    <f:decorate template="WEB-INF/templates/template.xhtml">

        <h:form id="files_form">

            <div id="ajax_error_contaiter" style="background-color: red">
            </div>
            <p:dataTable id="files" value="#{dir_view.directoryViewModel}" widgetVar="files_table"
                         var="file" selectionMode="multiple" selection="#{dir_view.selectedItems}" emptyMessage="Directory is empty"
                         rowKey="#{file.safeFileId}" sortBy="#{file.lastModified}" sortOrder="descending" filteredValue="#{dir_view.filtered}"
                         paginator="true" rows="30">
                <c:facet name="header">

                    <p:messages redisplay="true" id="msgs"/>
                    <p:inputText value="#{dir_view.currentDirectory}" id="dir" style="width: 60%; margin-left: 10px"/>
                    <p:commandButton id="btn_refresh" action="#{dir_view.refresh}" update="@(form)" value="Refresh" style="width: 100px; margin-left: 10px"
                                     onclick="clearFilters()"/>
                    <p:commandButton id="btn_dir_up" action="#{dir_view.goUp}" update="@(form)" value="Dir up" style="width: 120px;  margin-left: 10px"
                                     onclick="clearFilters()" disabled="#{not dir_view.hasParent}"/>
                </c:facet>
                <p:column id="fname_col" headerText="Name" style="width: 50%" resizable="true" filterBy="#{file.name}" filterMatchMode="contains" sortBy="#{file.name}">
                    <f:fragment rendered="#{file.directory}">
                        <p:graphicImage style="margin-right: 10px" value="#{resource['images:folder.png']}"/>
                            <span>
                                #{file.name}
                            </span>
                    </f:fragment>
                    <h:outputLink target="_blank" value="get/#{of:encodeURL(file.name)}" rendered="#{not file.directory}" style="margin-left: 45px">
                        <c:param name="file" value="#{file.absolutePath}"/>
                        <c:param name="type" value="text"/>
                        <c:param name="view" value="#{dir_view.tryToViewInBrowser ? 'inplace' : 'download'}"/>
                        <c:param name="encoding" value="#{dir_view.selectedEncoding}"/>
                        #{file.name}
                    </h:outputLink>
                </p:column>
                <p:column id="lastmod_col" headerText="Last modif" style="width: 120px" sortBy="#{file.lastModified}">
                    <h:outputText value="#{file.lastModified}" converter="LongTimeConverter"/>
                </p:column>
                <p:column id="size_col" headerText="Size" style="width: 117px" sortBy="#{file.size}">
                    <h:outputText value="#{file.file}" converter="FileSizeConverter"/>
                </p:column>
                <p:column id="action_col" style="width: 100px">
                    <p:commandButton icon="ui-icon-close" onclick="widget_del_confirm.show()" label="Delete file">
                        <c:setPropertyActionListener value="#{file}" target="#{dir_view.singleItem}"/>
                    </p:commandButton>
                    <p:button icon="ui-icon-pencil" onclick="window.open('edit.faces?file=#{of:encodeURL(file.absolutePath)}')" rendered="#{file.editable}" alt="Edit"/>
                    <p:commandButton icon="ui-icon-circle-triangle-e" process="files, @this" action="#{dir_view.execute}" update="files" rendered="#{file.executable}"
                                     alt="Run script">
                        <c:setPropertyActionListener value="#{file}" target="#{dir_view.singleItem}"/>
                    </p:commandButton>
                </p:column>
                <p:ajax event="rowDblselect" process="@(form)" listener="#{dir_view.selectDirectory}" update=":files_form" onstart="clearFilters()"/>
                <c:facet name="footer">
                    <f:remove>
                        <p:panelGrid columns="4" style="width: 50%">
                        </p:panelGrid>
                    </f:remove>

                    <div id="footer_control_panel" style="text-align: left; width: 50%">
                        <p:outputLabel for="combo_encoding">Files charset</p:outputLabel>
                        <p:selectOneMenu value="#{dir_view.selectedEncoding}" id="combo_encoding">
                            <c:selectItems value="#{cu:getAvailableCharsets()}"/>
                            <p:ajax event="change" process="combo_encoding" update="files"/>
                        </p:selectOneMenu>
                        <p:outputLabel for="vInB" value="View in browser" style="margin-left: 15px"/>
                        <p:selectBooleanCheckbox value="#{dir_view.tryToViewInBrowser}" id="vInB" style="margin-left: 5px">
                            <p:ajax update="files" global="false"/>
                        </p:selectBooleanCheckbox>
                    </div>
                </c:facet>

            </p:dataTable>

            <div style="overflow: hidden">
                <p:fileUpload id="file_uploader" fileUploadListener="#{dir_view.handleFileUpload}" mode="advanced" update="files file_messages file_uploader"
                              auto="true" label="Choose files to upload" uploadLabel="Upload files"
                              style="float: left; width: 80%"/>

                <div style="float: left; width: 20%;">
                    <p:commandButton value="Delete selected" onclick="clearFilters(); widget_del_confirm_mult.show();"
                                     style="margin-left: 30px; margin-top: 10px"/>
                </div>
            </div>
            <p:growl id="file_messages" showDetail="true" autoUpdate="false" life="800"/>
            <p:blockUI id="table_blocker" trigger="files:btn_refresh,files:btn_dir_up,files" block="files">
                <p:graphicImage value="#{resource['images:icon_waiting.gif']}"/>
            </p:blockUI>
            <f:remove>
                <p:contextMenu>
                    <p:menuitem actionListener="#{dir_view.deleteSelected}" update="files" value="Delete selected"/>
                    <p:menuitem target="_blank" value="Edit" outcome="edit.faces">
                        <c:param name="file" value="#{dir_view.selectedItems[0].absolutePath}"/>
                    </p:menuitem>
                </p:contextMenu>
            </f:remove>

            <p:confirmDialog id="delete_conf_dialog_mult" message="Delete selected item(s)?" header="Need confirmation" severity="alert" widgetVar="widget_del_confirm_mult">
                <p:commandButton id="btn_del_conf_mult" value="Delete" process="files, btn_del_conf_mult" update="files file_messages" oncomplete="widget_del_confirm_mult.hide()"
                                 action="#{dir_view.deleteSelected()}"/>
                <p:commandButton id="btn_cancel_conf_mult" value="Cancel" onclick="widget_del_confirm_mult.hide()"/>
            </p:confirmDialog>
            <p:confirmDialog id="delete_conf_dialog" message="Delete selected item?" header="Need confirmation" severity="alert" widgetVar="widget_del_confirm">
                <p:commandButton id="btn_del_conf" value="Delete" process="files, btn_del_conf" update="files file_messages" oncomplete="widget_del_confirm.hide()"
                                 action="#{dir_view.delete()}"/>
                <p:commandButton id="btn_cancel_conf" value="Cancel" onclick="widget_del_confirm.hide()"/>
            </p:confirmDialog>
        </h:form>
    </f:decorate>
</h:body>
</html>